xquery version "1.0";
(: $Id: guess.xql 7119 2008-01-06 11:52:08Z wolfgang_m $ :)

import module namespace request="http://exist-db.org/xquery/request";
import module namespace session="http://exist-db.org/xquery/session";
import module namespace util="http://exist-db.org/xquery/util";



declare function local:execfunction() as node()?
{
	session: create(),
	let $qselect := request:get-parameter("qselect", ())
	return
		if ($qselect eq "query1") then
			local:query1()
		else if ($qselect eq "query2") then
			local:query2()
		else if ($qselect eq "query3") then
			local:query3()
		else if ($qselect eq "query4") then
			local:query4()
		else if ($qselect eq "query5") then
			local:query5()
		else if ($qselect eq "query6") then
			local:query6()
		else if ($qselect eq "query7") then
			local:query7()
		else if ($qselect eq "query8") then
			local:query8()
		else
			<p> Nothing to display (yet)... select a query! </p>
};


declare function local:query1() as node()*
{
	<table>
	{
	for $i in collection("wikifonia2")//movement-title
	return <tr><td>{$i}</td></tr>
	}
	</table>
};

declare function local:query2() as node()*
{
	<p> At this moment there are
	{
		count(collection("wikifonia2"))
	}
	song in the Wikifonia database.</p>
};

declare function local:query3() as node()*
{
	<table>
	<tr><td><b> Song </b></td>
		<td><b> Note count </b></td>
	</tr>
	{
	for $i in collection("wikifonia2")
	let $j := count($i//note)
	order by $j descending
	return <tr>
		<td>{$i//movement-title/text()}</td>
		<td>{$j}</td>
		</tr>
	}
	</table>
};


declare function local:query4() as node()*
{
	<table>
	<tr><td><b> Artist </b></td>
		<td><b> Songs </b></td>
	</tr>
	{
		let $multiset := collection("wikifonia2")//creator[@type="composer"]/lower-case(text())
		let $set := distinct-values($multiset)
		for $i in $set
		let $c := count(index-of($multiset, $i))
		order by $c descending, $i
		return ( if ($c gt 1) then
				<tr>
				<td valign="top">{$i}</td>
				<td>	<table>
					{ for $j in collection("wikifonia2")//creator[@type="composer"][lower-case(text()) eq $i]/ancestor-or-self::score-partwise//movement-title
					return <tr><td>{$j/text()}</td></tr> }
					</table>
				</td>
				</tr>
			else ''
			)
	}
	</table>
};


declare function local:query5() as node()*
{
	<table>
	<tr><td><b> Time Signature </b></td>
		<td><b> Count </b></td>
		<td><b> Percentage </b></td>
	</tr>
	{
	let $t := collection("wikifonia2")//time
	let $nrt := count($t)
	let $b := distinct-values($t/beats)
	let $bt := distinct-values($t/beat-type)

	for $i in $b
	for $j in $bt
	let $c := count($t[beats eq $i][beat-type eq $j])
	let $p := $c div $nrt * 100
	order by $p descending
	return ( if ($c gt 0) then
			<tr>
				<td>{$i} / {$j}</td>
				<td>{$c}</td>
				<td>{$p}</td>
			</tr>
		else ''
		)
	}
	</table>
};


declare function local:query6() as node()*
{
	<table>
	<tr><td><b> Flat(-) / Sharp(+) </b></td>
		<td><b> Mode </b></td>
		<td><b> Count </b></td>
		<td><b> Percentage </b></td>
	</tr>
	{
	let $keys := collection("wikifonia2")//key
	let $t := count($keys)
	for $i in (-7 to 7)
	for $j in ("minor","major")
	let $c := count($keys[fifths eq string($i)][mode eq $j])
	let $p := $c div $t * 100
	order by $p descending
	return ( if ($c gt 0) then
			<tr>
				<td>{$i}</td>
				<td>{$j}</td>
				<td>{$c}</td>
				<td>{$p}</td>
			</tr>
		else ''
		)
	}
	</table>
};


declare function local:query7() as node()*
{
	<table>
	<tr><td><b> Song </b></td>
		<td><b> Composer </b></td>
		<td><b> Lyrics </b></td>
	</tr>
	{
	for $i in collection("wikifonia2")
	let $tit := $i//movement-title/text()
	let $aut := $i//creator[@type="composer"]/text()
	return
	<tr>
		<td valign="top">{$tit}</td>
		<td valign="top">{$aut}</td>
		<td>
			<table>
			{
			let $lyr := $i//lyric						(: all lyrics :)
			let $number := $lyr/@number
			let $nrv := if(empty($number)) then 1 else xs:integer(max($number))			(: nr of verses :)
													(: special case: only one verse, then no number argument present :)
			for $cur in (1 to $nrv)			(: iterate all verses, $cur is current verse. :)
			let $ver := $lyr[ if($nrv gt 1) then @number = $cur else true() ]/text		(: all <text> elements, corresponding to syllables :)
			let $s := string-join( for $syl in $ver return concat($syl/text(), if ($syl/../syllabic = ('begin','middle')) then '' else ' '), '')
			return 
				( if (not(empty($lyr))) then
					<tr><td valign="top"><b>{$cur}</b></td><td>{$s}</td></tr>		(: only if there are lyrics :)
				else
					<tr><td></td><td></td></tr>
				)
			}
			</table>
		</td>
	</tr>	

	}
	</table>
};


declare function local:query8() as node()*
{
	<table>
	{
	let $f := request:get-parameter("qfifths", ())
	let $m := request:get-parameter("qmode", ())
	for $i in collection("wikifonia2")//key[fifths/text() eq $f][mode/text() eq $m]
	return <tr><td>{$i/ancestor-or-self::score-partwise//movement-title}</td></tr>
	}
	</table>
};



<html>
    <head><title>ISMIR08 XQuery Demo</title></head>
    <body>


<h1 align="center">Query Wikifonia</h1>

<hr />


<b> Please choose which query you want to execute: </b>


<table>
<tr>

<td>
<form action="{session:encode-url(request:get-uri())}">
	<input type="hidden" name="qselect" value="query1"/>
	<input type="submit" value="1. Get titles"/>
</form>
</td>

<td>
<form action="{session:encode-url(request:get-uri())}">
	<input type="hidden" name="qselect" value="query2"/>
	<input type="submit" value="2. DB size"/>
</form>
</td>

<td>
<form action="{session:encode-url(request:get-uri())}">
	<input type="hidden" name="qselect" value="query3"/>
	<input type="submit" value="3. Get notecounts"/>
</form>
</td>

<td>
<form action="{session:encode-url(request:get-uri())}">
	<input type="hidden" name="qselect" value="query4"/>
	<input type="submit" value="4. Most occurring authors"/>
</form>
</td>

<td>
<form action="{session:encode-url(request:get-uri())}">
	<input type="hidden" name="qselect" value="query5"/>
	<input type="submit" value="5. Time sig stats"/>
</form>
</td>

<td>
<form action="{session:encode-url(request:get-uri())}">
	<input type="hidden" name="qselect" value="query6"/>
	<input type="submit" value="6. Key sig stats"/>
</form>
</td>

<td>
<form action="{session:encode-url(request:get-uri())}">
	<input type="hidden" name="qselect" value="query7"/>
	<input type="submit" value="7. Lyric library"/>
</form>
</td>

</tr>
</table>
<table>
<tr>

<td>
<form action="{session:encode-url(request:get-uri())}">
	<input type="hidden" name="qselect" value="query8"/>
	Nr. flat(-)/sharp(+):
	<select name="qfifths">
	{ for $i in (-7 to 7)
		return <option value="{$i}">{$i}</option>
	}
	</select>
	; Mode:
	<select type="select" name="qmode">
	{ for $i in ("minor","major")
		return <option value="{$i}">{$i}</option>
	}
	</select>	
	<input type="submit" value="find songs in this key"/>
</form>
</td>


<td>
<form action="{session:encode-url(request:get-uri())}">
	<input type="hidden" name="qselect" value="reset"/>
	<input type="submit" value="reset results"/>
</form>
</td>

</tr>
</table>

<hr />
<h1 align="center"> Results:</h1>

{ local:execfunction() }

    </body>
</html>




